<?php

namespace app\admin\model;

use fast\Http;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Auction extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'auction';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'status_text'
    ];


    public function getStatusList()
    {
        return [0 => '待开始', 1 => '进行中', 2 => '已结束', 3 => '已暂缓', 4 => '已中止', 5 => '已撤回', 6 => '已暂停'];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }



    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['start_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['end_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }



    public function getcity($lat,$lng)
    {

        $url = 'https://restapi.amap.com/v3/geocode/regeo';

        $data = [
            'output'=>'json',
            'key'=>'84188bdb74e00130b9520d94d13f6936',
            'location'=>$lng.','.$lat
        ];

        $res = Http::get($url,$data);

        $res = json_decode($res);

        if ($res->status!=1){
            return [
                'code'=>0,
                'data'=>$data,
                'msg'=>'获取地址失败'
            ];
        }
        $district = $res->regeocode->addressComponent->district;
        $citycode = $res->regeocode->addressComponent->citycode;

        $district_info = Area::get(['name'=>$district,'level'=>3,'code'=>$citycode]);

        if (!$district_info){
            return [
                'code'=>0,
                'data'=>$data,
                'msg'=>'获取地址失败'
            ];
        }

        $city_info = Area::get($district_info->pid);

        $province_info = Area::get($city_info->pid);

        $data = [
            'province_id'=>$province_info->id,
            'city_id'=>$city_info->id,
            'district_id'=>$district_info->id,

            'province_name'=>$province_info['name'],
            'city_name'=>$city_info['name'],
            'district_iname'=>$district_info['name'],
        ];

        return [
            'code'=>1,
            'data'=>$data,
            'msg'=>'success'
        ];
    }
}
